package com.example.servlet.user;

import com.example.entity.User;
import com.example.service.UserService;
import com.example.service.impl.UserServiceImpl;
import com.example.utils.MybatisUtil;
import com.example.utils.ThymeleafUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;

@WebServlet("/user_profile")
public class uProfileServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Context context = new Context();
        if (req.getSession().getAttribute("update-success") != null) {
            context.setVariable("update_success", true);
            req.getSession().removeAttribute("update-success");
        }

        User user = (User) req.getSession().getAttribute("user");
        context.setVariable("user_id", user.getU_id());
        context.setVariable("name", user.getU_name());
        context.setVariable("sex", user.getSex());
        context.setVariable("ID_type", user.getID_type());
        context.setVariable("ID_card", user.getID_card());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        context.setVariable("birthday", formatter.format(user.getBirthday()));
        context.setVariable("phone_number", user.getU_phone());
        context.setVariable("account", user.getU_account());
        context.setVariable("password", user.getU_password());
        context.setVariable("grade", user.getGrade());
        context.setVariable("email", user.getEmail());
        context.setVariable("city", user.getCity());
        // 图片是否存在判断
        String imagePath = getServletContext().getRealPath("/static/images/" + user.getU_id() + ".jpg");
        File imageFile = new File(imagePath);
        boolean imageExists = imageFile.exists();
        context.setVariable("imageExists", imageExists);
        ThymeleafUtil.process("user_profile.html", context, resp.getWriter());
    }
}
